<%@ page import="com.actimind.actitime.Keys,
                 com.actimind.actitime.util.TimeUtils,
                 com.actimind.actitime.util.NameUtil,
                 org.apache.struts.util.ResponseUtils,
                 com.actimind.actitime.resources.TaskListSortConstants,
                 com.actimind.actitime.task.TimeCommentsComparators"%>
<%@ page errorPage="/error.jsp" %>
<%@ taglib uri="/WEB-INF/at.tld" prefix="at" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<bean:define id="form" name="TaskDetailsForm" type="com.actimind.actitime.task.TaskDetailsForm"/>
<bean:define id="task" name="form" property="task" type="com.actimind.actitime.beans.TaskBean"/>
<bean:define id="executors" name="form" property="executorsSummary" type="com.actimind.actitime.resources.TaskExecutorsSummary"/>
<bean:define id="pager" name="<%=Keys.PAGER%>" scope="session" type="com.actimind.actitime.util.Pager" />
<%
    String status = form.getTask().isCompleted() ? "Completed" : "Open";
    String title = "View " + status + " Task";
%>
<html>
<head>
    <title><bean:write name="title-prefix" scope="application"/> <%=title%></title>

    <script language="JavaScript" src="../js/at.js"></script>
    <script language="JavaScript">
        var basicURL = '<html:rewrite page='<%="/tasks/task_details.do?origin=" + form.getOrigin() + "&taskId=" + form.getTaskId() + "&popupMode=" + form.isPopupMode()%>'/>';
        var anchor = "#time_comments";

        function changeSortOrder(sortBy) {
            var newSortOrder = '<%=TimeCommentsComparators.SORT_ORDER_ASC%>';
            if(sortBy == '<%=form.getSortBy()%>') {
                var currentSortOrder = '<%=form.getSortOrder()%>';
                if(currentSortOrder == '<%=TimeCommentsComparators.SORT_ORDER_ASC%>')
                    newSortOrder = '<%=TimeCommentsComparators.SORT_ORDER_DESC%>';
                else
                    newSortOrder = '<%=TimeCommentsComparators.SORT_ORDER_ASC%>';
            } else
            {
                if(sortBy == '<%=TimeCommentsComparators.SORT_BY_DATE%>')
                    newSortOrder = '<%=TimeCommentsComparators.SORT_ORDER_DESC%>';
            }
            return newSortOrder;
        }

        function sortList(sortBy)
        {
            var newSortOrder = changeSortOrder(sortBy);
            var url = basicURL + '&sortBy=' + sortBy + '&sortOrder=' + newSortOrder + '&recordsPerPage=' +
                    '<%=form.getRecordsPerPage()%>' + anchor;
            window.location.href = url;
        }

        function page(recordNum)
        {
            var url = basicURL + '&currentRecord=' + recordNum + '&recordsPerPage=' +
                    '<%=form.getRecordsPerPage()%>' + anchor;
            window.location.href = url;
        }

        function setPageLimit(pageLimit)
        {
            var url = basicURL + '&sortBy=' + '<%=form.getSortBy()%>' + '&sortOrder=' + '<%=form.getSortOrder()%>' + '&recordsPerPage=' +
                    pageLimit + anchor;
            window.location.href = url;
        }

    </script>
    <link rel="stylesheet" href="../css/at.css" type="text/css">
</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<form>
<logic:equal name="form" property="popupMode" value="false">
<jsp:include page="../top.jsp" flush="false">
    <jsp:param name="pageSection" value="tasks" />
    <jsp:param name="pageAction" value="task_details" />
</jsp:include>
</logic:equal>
<logic:equal name="form" property="popupMode" value="true"><br></logic:equal>

<table cellspacing="0" cellpadding="5" align="center" valign="top" border="0" bgcolor="#ffffff" width="770">
<tr>
    <td class="pagetitle">
        <%=title%>
    </td>
</tr>
<tr>
    <td>
        <at:errors />
        <at:messages id="message" message="true">
            <span class="successmsg"><bean:write name="message"/></span>
        </at:messages>
    </td>
</tr>
<tr>
    <td class="formbuttonpane">
    <table width="100%" cellpadding="3" cellspacing="0" valign="top" border="0">
    <tr>
        <td width="22%" class="formblocktitle">You have selected task:</td>
        <td class="formfieldtitle"><span class="textemphasis"><%=task.getName()%></span></td>
        <td align="right" nowrap valign="top">
            <logic:equal name="form" property="ableToEditTask" value="true">
            <html:link href="<%=(task.isCompleted() ? "ctaskedit.do" : "otaskedit.do") + "?taskId=" + task.getId()%>">Edit Task Parameters</html:link>
            </logic:equal>
        </td>
    </tr>
    <tr>
        <td class="formblocktitle">Task status:</td>
        <td class="formfieldtitle">
        <span class="<%=task.isCompleted() ? "completed_task_status" : "opened_task_status"%>"><%=task.isCompleted() ? "Completed" : "Open"%></span>
        </td>
        <td></td>
    </tr>
    <logic:equal property="completed" value="true" name="task">
    <tr>
        <td class="formblocktitle">Completion date:</td>
        <td class="formfieldtitle">
        <span class="completed_task_status"><%=task.getCompletionDate("MMM dd, yyyy")%></span>
        </td>
        <td></td>
    </tr>
    </logic:equal>
    <tr>
        <td class="formblocktitle">Project:</td>
        <td class="formfieldtitle"><%=task.getProject().getName()%></td>
        <td></td>
    </tr>
    <tr>
        <td class="formblocktitle">Customer:</td>
        <td class="formfieldtitle"><%=task.getProject().getCustomer().getName()%></td>
        <td></td>
    </tr>
    <tr>
        <td class="formblocktitle">Deadline:</td>
        <td class="formfieldtitle"><%=task.getPrintableDeadlineDate()%></td>
        <td></td>
    </tr>
    <tr>
        <td class="formblocktitle">Billing type:</td>
        <td class="formfieldtitle"><%= task.getBillingType() == null ? "Non-Billable" : ResponseUtils.filter(task.getBillingType().getName()) %></td>
        <td></td>
    </tr>
    <tr>
        <td class="formblocktitle">Task description:</td>
        <td class="formfieldtitle">
        <logic:empty name="task" property="description">N/A</logic:empty>
        <%=task.getDescription()%>
        </td>
        <td></td>
    </tr>
    <tr>
        <td class="formblocktitle" valign="top">Executors / Spent time:</td>
        <td class="formfieldtitle" valign="top">
        <logic:equal name="executors" property="executorsAvailable" value="false">No time and/or comments reported</logic:equal>
        <logic:equal name="executors" property="executorsAvailable" value="true">
        <table width="50%" cellpadding="0" cellspacing="0">
        <logic:iterate id="record" name="executors" property="records" type="com.actimind.actitime.resources.TaskExecutorsSummary.SummaryRecord">
        <tr>
            <td width="45%" class="formfieldtitle" nowrap><bean:write name="record" property="formattedName"/></td>
            <td width="2%" class="formfieldtitle">&nbsp;:&nbsp;</td>
            <td width="10%" align="right" nowrap><span class="formfieldtitle"><%=TimeUtils.formatWork(record.getSpentTime())%></span></td>
            <td class="formfieldtitle" nowrap width="43%">
                <logic:equal name="record" property="spentTime" value="0">
                <logic:equal name="record" property="commentsAvailable" value="true">&nbsp;(comments only)</logic:equal>
                </logic:equal>
            </td>
        </tr>
        </logic:iterate>
	<% if (executors.getRecords().size() > 1) { %>
        <tr>
            <td colspan="3" bgcolor="#C0C0C0"><html:img page="/img/pixel.gif" height="1"/></td>
            <td></td>
        </tr>
        <tr>
            <td class="formfieldtitle" nowrap>Total</td>
            <td class="formfieldtitle">&nbsp;:&nbsp;</td>
            <td align="right" nowrap><span class="formfieldtitle"><%=TimeUtils.formatWork(executors.getTotal())%></span></td>
            <td></td>
        </tr>
	<% } %>
        </table>
        </logic:equal>
        </td>
	    <td rowspan="2" valign="bottom" align="right">
        <logic:equal name="form" property="ableToDeleteTask" value="true">
            <input type="button" value="Delete This Task" onclick="javascript: if (confirm('Are you sure you want to delete this task?')) document.location.href='<html:rewrite href='<%="otaskdelete.do?taskId=" + task.getId()%>'/>'" />
        </logic:equal>
        </td>
    </tr>
    <tr>
        <td class="formblocktitle">Task creation date:</td>
        <td class="formfieldtitle"><%=task.getPrintableCreationDate()%></td>
    </tr>
    </table>
    </td>
</tr>
</table>
<a name="time_comments"></a><br>
<table cellspacing="1" cellpadding="3" align="center" valign="top" border="0" width="770">
    <tr>
        <td colspan="4" class="formblocktitle">Reported Time and Comments:</td>
    </tr>
     <tr>
            <td nowrap=1 class="listtblcolheader" align="center" width="14%">
                <logic:equal name="form" property="timeCommentsSet" value="true">
                    <a class="listtblcolheader" href="javascript:sortList('<%=TimeCommentsComparators.SORT_BY_DATE%>');">Date</a>
                    <logic:equal name="form" property="sortBy" value="<%=TimeCommentsComparators.SORT_BY_DATE%>">
                        <logic:equal name="form" property="sortOrder" value="<%=TimeCommentsComparators.SORT_ORDER_ASC%>">
                            <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                        </logic:equal>
                        <logic:equal name="form" property="sortOrder" value="<%=TimeCommentsComparators.SORT_ORDER_DESC%>">
                            <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                        </logic:equal>
                    </logic:equal>
                </logic:equal>
                <logic:equal name="form" property="timeCommentsSet" value="false">Date</logic:equal>
                <html:img page="/img/pixel.gif" width="1" height="25" align="absmiddle"/>
            </td>
            <td nowrap=1 class="listtblcolheader" align="center" width="22%">
                <logic:equal name="form" property="timeCommentsSet" value="true">
                    <a class="listtblcolheader" href="javascript:sortList('<%=TimeCommentsComparators.SORT_BY_USERNAME%>');">User</a>
                    <logic:equal name="form" property="sortBy" value="<%=TimeCommentsComparators.SORT_BY_USERNAME%>">
                        <logic:equal name="form" property="sortOrder" value="<%=TimeCommentsComparators.SORT_ORDER_ASC%>">
                            <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                        </logic:equal>
                        <logic:equal name="form" property="sortOrder" value="<%=TimeCommentsComparators.SORT_ORDER_DESC%>">
                            <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                        </logic:equal>
                    </logic:equal>
                </logic:equal>
                <logic:equal name="form" property="timeCommentsSet" value="false">User</logic:equal>
            </td>
            <td nowrap=1 class="listtblcolheader" align="center" width="8%">
                Time
            </td>
            <td nowrap=1 class="listtblcolheader" align="center" width="56%">
                Comments
            </td>
        </tr>
        <logic:equal name="form" property="timeCommentsSet" value="false">
        <tr>
            <td colspan="4" align="center" class="formbuttonpane"><br><span class="text">There are no time and/or comments reported for the task.</span><br>&nbsp;</td>
        </tr>
        </logic:equal>
        <logic:equal name="form" property="timeCommentsSet" value="true">
        <logic:iterate id="timeCommentsRecord" name="form" property="reportedTimeComments" type="com.actimind.actitime.resources.TimeCommentsRecord">
        <tr>
            <td class="listtblcell"><%=TimeUtils.toString(timeCommentsRecord.getEventDate(), "MMM dd, yyyy")%></td>
            <td class="listtblcell"><bean:write name="timeCommentsRecord" property="formattedName" /></td>
            <td class="listtblcell" align="center"><bean:write name="timeCommentsRecord" property="formattedActuals"/></td>
            <td class="listtblcell">
                <bean:write name="timeCommentsRecord" property="formattedComments" filter="false" />&nbsp;
            </td>
        </tr>
        </logic:iterate>
        <tr><td><img src="../img/pixel.gif" width="1" height="1"></td></tr>
        <tr>
            <td colspan="4">
                <table border="0" cellspacing="0" cellpadding="0"  align="center" width="770">
                    <tr>
                        <td class="text">There
                            <logic:greaterThan  name="form" property="recordCount" value="1">
                                are <b><bean:write name="form" property="recordCount" /></b> records
                            </logic:greaterThan>
                            <logic:equal name="form" property="recordCount" value="1">
                                is <b><bean:write name="form" property="recordCount" /></b> record
                            </logic:equal>
                         </td>
                        <%  if (pager.getCurrentPages().length > 1) {%>
                            <td class="text" align="center">
                                <logic:notEmpty name="pager" property="previousPageFirstRecordNumber">
                                <a href="javascript:page('<%=pager.getPreviousPageFirstRecordNumber().intValue()%>');">
                                Previous
                                </a>
                                </logic:notEmpty>
                                <logic:empty name="pager" property="previousPageFirstRecordNumber">
                                Previous
                                </logic:empty>
                                --&nbsp;&nbsp;&nbsp;
                                <logic:iterate name="pager" property="currentPages" id="curPage">
                                    <logic:equal value="true" name="curPage" property="current">
                                        <bean:write name="curPage"/>
                                    </logic:equal>
                                    <logic:notEqual value="true" name="curPage" property="current">
                                        <a href="javascript:page('<%=((com.actimind.actitime.util.Pager.Page)curPage).getFirstRecordNumber()%>');"><bean:write name="curPage"/></a>
                                    </logic:notEqual>
                                    &nbsp;
                                </logic:iterate>
                                &nbsp;&nbsp;
                                --
                                <logic:notEmpty name="pager" property="nextPageFirstRecordNumber">
                                <a href="javascript:page('<%=pager.getNextPageFirstRecordNumber().intValue()%>');">
                                Next
                                </a>
                                </logic:notEmpty>
                                <logic:empty name="pager" property="nextPageFirstRecordNumber">
                                Next
                                </logic:empty>
                            </td>
                        <%  } //if %>
                            <td class="text" align="right">Show&nbsp;&nbsp;
                                <html:select name="pager" property="recordsPerPage"
                                        onchange="javascript:setPageLimit(this.options[this.selectedIndex].value);">
                                    <html:options name="pager" property="recordsPerPageCollection"/>
                                </html:select>&nbsp;&nbsp;&nbsp;records on a page
                                <script>sel = document.forms[0].elements['recordsPerPage']; for (i = 0; i < sel.options.length; i++) if (sel.options[i].value == document.forms[0].elements['recordsPerPage']) {sel.selectedIndex = i; break;};</script>
                            </td>
                    </tr>
                </table>
            </td>
        </tr>
        </logic:equal>
</table>

<table align="center" valign="top" border="0" bgcolor="#ffffff" width="770" cellpadding="0" cellspacing="0">
<logic:equal name="form" property="popupMode" value="true">
<tr>
    <td align="center"><html:img page="/img/pixel.gif" height="10"/></td>
</tr>
<tr>
    <td align="center" bgcolor="#e8e8e8"><a href="javascript:window.close();">Close Window</a><html:img page="/img/pixel.gif" height="30" align="absmiddle"/></td>
</tr>
</logic:equal>
</table>
<p>
</form>
</body>
</html>